<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/11/25
 * Time: 15:29
 */
namespace App\Http\Controllers\Admin;

use App\Service\Admin\VideoService;
use App\Constant\ErrorConstant;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VideoController extends BaseController
{
    protected $service;

    public function __construct(Request $request, VideoService $service)
    {
        parent::__construct($request);

        $this->service  =   $service;
    }

    /**
     * 短视频列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listVideo()
    {
        $page   =   (int) $this->request->input('page', 1);
        $limit  =   $this->request->input('limit', NULL);
        [$flag, $result]    =   $this->service->videoList($limit, $page);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_LIST_ERROR, $result);
        }
    }

    /**
     * 短视频添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addVideo()
    {
        $validator  =   Validator::make($this->request->post(), [
            'short_name'    =>  'required|string',
            'short_cover'   =>  'required|string',
            'video_url'     =>  'required|string',
            'good_array'    =>  'required|array',
            'good_array.*'  =>  'required|integer|exists:mini_product,id'
        ], [
            'short_name.required'   =>  '短视频名称不能为空',
            'short_name.string'     =>  '短视频名称必须为字符',

            'short_cover.required'  =>  '短视频封面不能为空',
            'short_cover.string'    =>  '短视频封面必须为字符',

            'video_url.required'    =>  '短视频地址不能为空',
            'video_url.string'      =>  '短视频地址必须为字符',

            'good_array.required'   =>  '商品数组不能为空',
            'good_array.array'      =>  '商品数组必须为数组',
            'good_array.*.required' =>  '商品ID不能为空',
            'good_array.*.integer'  =>  '商品ID必须为整型',
            'good_array.*.exists'   =>  '商品不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->videoAdd($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_ADD_ERROR, $result);
        }
    }

    /**
     * 短视频编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function editVideo()
    {
        $validator   =   Validator::make($this->request->post(), [
            'short_name'    =>  'required|string',
            'short_cover'   =>  'required|string',
            'video_url'     =>  'required|string',
            'good_array'    =>  'required|array',
            'good_array.*'  =>  'required|integer|exists:mini_product,id',
            'video_id'      =>  'required|integer|exists:mini_short_video,id'
        ], [
            'short_name.required'   =>  '短视频名称不能为空',
            'short_name.string'     =>  '短视频名称必须为字符',

            'short_cover.required'  =>  '短视频封面不能为空',
            'short_cover.string'    =>  '短视频封面必须为字符',

            'video_url.required'    =>  '短视频地址不能为空',
            'video_url.string'      =>  '短视频地址必须为字符',

            'good_array.required'   =>  '商品数组不能为空',
            'good_array.array'      =>  '商品数组必须为数组',
            'good_array.*.required' =>  '商品ID不能为空',
            'good_array.*.integer'  =>  '商品ID必须为整型',
            'good_array.*.exists'   =>  '商品不存在',

            'video_id.required'     =>  '短视频ID不能为空',
            'video_id.integer'      =>  '短视频ID必须为整型',
            'video_id.exists'       =>  '短视频不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_EDIT_ERROR, $validator->errors()->first());
        }

        $video_id   =   (int) $this->request->post('video_id');
        [$flag, $result]    =   $this->service->videoEdit($this->request->post(), $video_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_EDIT_ERROR, $result);
        }
    }

    /**
     * 短视频删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteVideo()
    {
        $validator   =   Validator::make($this->request->post(), [
            'video_id'  =>  'required|integer|exists:mini_short_video,id'
        ], [
            'video_id.required' =>  '短视频ID不能为空',
            'video_id.integer'  =>  '短视频ID必须为整型',
            'video_id.exists'   =>  '短视频不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_DELETE_ERROR, $validator->errors()->first());
        }

        $video_id   =   (int) $this->request->post('video_id');
        [$flag, $result]    =   $this->service->videoDelete($video_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_DELETE_ERROR, $result);
        }
    }

    /**
     * 短视频上传
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadVideo()
    {
        $validator  =   Validator::make($this->request->post(), [
            'file'  =>  'required|mimetypes:video/avi,video/mpeg,video/quicktime',
        ], [
            'file.required' =>  '上传视频不能为空',
            'file.mimetypes'=>  '上传视频格式错误'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_UPLOAD_ERROR, $validator->errors()->first());
        }

        $file   =   $this->request->all();
        [$flag, $result] = $this->service->videoUpload($file['file']);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_VIDEO_UPLOAD_ERROR, $result);
        }
    }
}
